﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.ToolBoxes
{
    using System.Collections.ObjectModel;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Controls;
    using Probel.Mvvm.DataBinding;

    public class CreateDoctorViewModel : BaseViewModel
    {
        #region Fields

        private readonly ICommand createDoctorCommand;
        private readonly DoctorDto Doctor = new DoctorDto();
        private readonly IPatientService Service;

        private SpecialisationDto selectedSpecialisation;

        #endregion Fields

        #region Constructors

        public CreateDoctorViewModel(IErrorService errorService, IPatientService service, bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.Service = service;

            this.Specialisations = new ObservableCollection<SpecialisationDto>();

            this.createDoctorCommand = new RelayCommand(this.CreateDoctor, this.CanCreateDoctor);
        }

        #endregion Constructors

        #region Properties

        public string BoxNumber
        {
            get { return this.Doctor.Address.BoxNumber; }
            set
            {
                this.Doctor.Address.BoxNumber = value;
                this.OnPropertyChanged(() => BoxNumber);
            }
        }

        public string City
        {
            get { return this.Doctor.Address.City; }
            set
            {
                this.Doctor.Address.City = value;
                this.OnPropertyChanged(() => City);
            }
        }

        public ICommand CreateDoctorCommand
        {
            get { return this.createDoctorCommand; }
        }

        public string FirstName
        {
            get { return this.Doctor.FirstName; }
            set
            {
                this.Doctor.FirstName = value;
                this.OnPropertyChanged(() => FirstName);
            }
        }

        public string LastName
        {
            get { return this.Doctor.LastName; }
            set
            {
                this.Doctor.LastName = value;
                this.OnPropertyChanged(() => LastName);
            }
        }

        public string PostalCode
        {
            get { return this.Doctor.Address.PostalCode; }
            set
            {
                this.Doctor.Address.PostalCode = value;
                this.OnPropertyChanged(() => PostalCode);
            }
        }

        public string ProPhone
        {
            get { return this.Doctor.ProPhone; }
            set
            {
                this.Doctor.ProPhone = value;
                this.OnPropertyChanged(() => ProPhone);
            }
        }

        public SpecialisationDto SelectedSpecialisation
        {
            get { return this.Doctor.Specialisation; }
            set
            {
                this.Doctor.Specialisation = value;
                this.OnPropertyChanged(() => SelectedSpecialisation);
            }
        }

        public ObservableCollection<SpecialisationDto> Specialisations
        {
            get;
            private set;
        }

        public string Street
        {
            get { return this.Doctor.Address.Street; }
            set
            {
                this.Doctor.Address.Street = value;
                this.OnPropertyChanged(() => Street);
            }
        }

        public string StreetNumber
        {
            get { return this.Doctor.Address.StreetNumber; }
            set
            {
                this.Doctor.Address.StreetNumber = value;
                this.OnPropertyChanged(() => StreetNumber);
            }
        }

        #endregion Properties

        #region Methods

        public object BuildSpecialisationPopupViewModel()
        {
            return new SpecialisationPopupViewModel(this, this.ErrorService, this.Service, this.IsAsync);
        }

        public void Initialise()
        {
            this.Async(() => this.Service.GetAllSpecialisations(),
                        r => this.Specialisations.Refill(r));
        }

        private bool CanCreateDoctor()
        {
            return !string.IsNullOrWhiteSpace(this.Doctor.FirstName)
                && !string.IsNullOrWhiteSpace(this.Doctor.LastName)
                && this.SelectedSpecialisation != null;
        }

        private void CreateDoctor()
        {
            this.Service.Create(this.Doctor);
        }

        #endregion Methods
    }
}